
//二叉树类
class TreeNode {
    val; //节点值
    left; //左子节点
    right; //右子节点
    //构造函数
    constructor(val, left = null, right = null) {
        this.val = val; //节点值
        this.left = left; //左子节点
        this.right = right; //右子节点
    }
}


let TreeNode1 = new TreeNode(1);
let TreeNode2 = new TreeNode(2);
let TreeNode3 = new TreeNode(3);
let TreeNode4 = new TreeNode(4);
let TreeNode5 = new TreeNode(5);
let TreeNode6 = new TreeNode(6);
let TreeNode7 = new TreeNode(7);
let TreeNode8 = new TreeNode(8);
let TreeNode9 = new TreeNode(9);

//构建二叉树
TreeNode1.left = TreeNode2;
TreeNode1.right = TreeNode3;
TreeNode2.left = TreeNode4;
TreeNode2.right = TreeNode5;
TreeNode3.left = TreeNode6;
TreeNode3.right = TreeNode7;
TreeNode4.left = TreeNode8;
TreeNode4.right = TreeNode9;

/**
 * 
 *       1
 *      / \
 *     2   3
 *    / \ / \
 *   4  5 6  7
 *  / \
 * 8   9
 * 
 */

console.log(TreeNode1);
let leftValue = TreeNode1.left.val;
console.log("未插入节点前,TreeNode1的左子节点值为:",leftValue);

//在二叉树中插入一个节点
let n1 = new TreeNode('n1'); //创建一个n1的节点
TreeNode1.left = n1; //将n1的节点插入到TreeNode1的左子节点
n1.left = TreeNode2; //将TreeNode2的节点插入到n1的左子节点


console.log("插入节点后,TreeNode1的左子节点值为:",TreeNode1.left.val);
console.log(leftValue == TreeNode1.left.left.val); //判断插入节点后,TreeNode1的左子节点值是否为n1


//在二叉树中删除一个节点
//找到需要删除的节点
TreeNode1.left = TreeNode2; //直接将TreeNode2的节点插入到TreeNode1的左子节点,覆盖掉原来的n1节点
console.log("删除节点后,TreeNode1的左子节点值为:",TreeNode1.left.val);
console.log(leftValue == TreeNode1.left.val); //判断删除节点后,TreeNode1的左子节点值是否为TreeNode2